<template>
  <div class="recommend-card">
    <div class="cover-box"
         @mouseenter="isHover = true"
         @mouseleave="isHover = false">
      <img
        v-if="!isHover"
        :src="`http://localhost:8000/api/bili_img_proxy/?url=${encodeURIComponent(props.coverUrl)}`"
        class="cover"
        alt="视频封面"
        style="cursor:pointer;"
      />
      <iframe
        v-if="isHover"
        :src="bilibiliIframeUrl"
        frameborder="0"
        allowfullscreen
        class="bilibili-iframe"
      ></iframe>
    </div>
    <div v-if="title" class="title" :title="title">{{ title }}</div>
    <div class="info-row">
      <span v-if="authorName" class="type">{{ authorName }}</span>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
const props = defineProps({
  coverUrl: { type: String, required: true },
  title: { type: String, required: true },
  authorName: { type: String, required: true },
  videoUrl: { type: String, required: false, default: '' }
})
const isHover = ref(false)
const bilibiliIframeUrl = computed(() => {
  const match = props.videoUrl.match(/bilibili\.com\/video\/(BV[\w]+)/i)
  if (match && match[1]) {
    return `https://player.bilibili.com/player.html?bvid=${match[1]}&autoplay=1`
  }
  return props.videoUrl
})
function openBilibili() {
  if (props.videoUrl) {
    window.open(props.videoUrl, '_blank')
  }
}
</script>

<style scoped>
.recommend-card {
  flex: 0 0 calc((100% - 48px) / 3); /* 3个一排，2个gap共48px */
  max-width: calc((100% - 48px) / 3);
  background: #fff;
  border-radius: 16px;
  overflow: hidden;
  box-shadow: 0 4px 16px rgba(0,0,0,0.10);
  margin: 0;
  display: flex;
  flex-direction: column;
  transition: box-shadow 0.2s, transform 0.2s;
  font-family: 'PingFang SC', 'HarmonyOS Sans', 'Helvetica Neue', Arial, sans-serif;
  cursor: pointer;
}
.recommend-card:hover {
  box-shadow: 0 8px 32px rgba(0,0,0,0.18);
  transform: translateY(-4px) scale(1.02);
}
.recommend-card:nth-child(3n) {
  margin-right: 0;
}
.cover-box {
  position: relative;
  width: 100%;
  aspect-ratio: 16/9;
  background: #f2f2f2;
  overflow: hidden;
  border-top-left-radius: 16px;
  border-top-right-radius: 16px;
}
.cover {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}
.duration {
  position: absolute;
  right: 10px;
  bottom: 10px;
  background: rgba(0,0,0,0.7);
  color: #fff;
  font-size: 13px;
  padding: 2px 8px;
  border-radius: 6px;
}
.title {
  font-size: 17px;
  color: #222;
  font-weight: bold;
  margin: 12px 16px 4px 16px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.desc {
  font-size: 14px;
  color: #666;
  margin: 0 16px 8px 16px;
  height: 38px;
  line-height: 19px;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}
.info-row {
  display: flex;
  align-items: center;
  margin: 0 16px 14px 16px;
  font-size: 13px;
  color: #999;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.type {
  color: #2c3e50;
  font-weight: 500;
  margin-right: 6px;
}
.dot {
  margin: 0 6px;
}
.time {
  color: #aaa;
}
.bilibili-iframe {
  width: 100%;
  height: 220px;
  border: none;
  margin-top: 8px;
  border-radius: 8px;
}
</style>
